package com.lin.manager.websocket.config;

import com.lin.manager.websocket.message.MessageHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

/**
 * @作者: 林江
 * @创建时间: 2024/4/17
 * @功能:
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(handler(), "/web")
                // 基于什么实现WebSocket的握手，可以自定义
                .addInterceptors(new HttpSessionHandshakeInterceptor())
                // vue或工具发送wesocket会检查同源策略
                .setAllowedOriginPatterns("*")
//                .withSockJS() // 当浏览器不适合webscoket协议的时候，会回退为长连接
//                .setTaskScheduler(new ThreadPoolTaskScheduler())
        ;
    }

    @Bean
    public MessageHandler handler() {
        return new MessageHandler();
    }
}
